# coding: UTF-8
# ----------
# Project_Name : pythonProject
# File_Name : huangcongbing
# Author : asus
# Date_Time:2022/4/22 16:13
# Description :
# ----------
from selenium import  webdriver
import time
from selenium.webdriver.common.by import By
def foo1():
    driver = webdriver.Chrome()
    driver.maximize_window()

    driver.get(r"http://news.baidu.com/")
    # 循环打开6个热点新闻
    for i in range(1,7):
        driver.find_element(By.XPATH,f'//*[@id="pane-news"]/div/ul/li[{i}]').click()
        time.sleep(3)
    # 获取所有窗口的handle (句柄)
    handles = driver.window_handles
    print(handles)
    #循环切换窗口
    for handle in handles:
        driver.switch_to.window(handle)
        #根据页面url或者title判断页面
        if driver.current_url =="https://news.cctv.com/2022/04/22/ARTIvl4KswVg2nPGggIBaY24220422.shtml":
            break
        time.sleep(1)
#浏览器切换顺序 是由浏览器决定的，每个都不一样


    driver.quit()


if __name__ == "__main__":
    foo1()